<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortalHtmlArea.php 450 2011-01-05 15:40:33Z steen.rabol $
 *
 */

Prado::using('System.Web.UI.WebControls.THtmlArea');
Prado::using('Application.Common.Data.PortalPageRecord');

class PortalHtmlArea extends THtmlArea
{
	private $_deployurl		= null;
	private $_deploypath	= null;


	public function __construct()
	{
		parent::__construct();
	}

	protected function getScriptDeploymentPath()
	{
		$assets				= $this->getApplication()->getAssetManager();
		$this->DeployURL	= parent::getScriptDeploymentPath();
		$this->DeployPath	= str_replace('//','/', $assets->getBasePath()) . '/' . basename($this->DeployURL);

		$this->getPage()->getClientScript()->registerScriptFile('tinybrowser',$this->DeployURL . '/tiny_mce/plugins/tinybrowser/tb_tinymce.js.php');

		//build the configuration
		$pp_root 		= str_replace("\\", "/",$this->Application->PortalBasePath) . "/";
		$pp_root 		= str_replace("//","/",$pp_root);
		$pp_lang		= $this->Application->Culture;
		$pp_rooturl		= $this->Application->PortalBaseUrl;
		$pp_relrooturl	= '/'; //$this->Application->PortalRelUrl;
		$pf				= "<?php\r\n";
		$pf				.= "\$pp_root =	'$pp_root';\r\n";
		$pf				.= "\$pp_rooturl =	'$pp_rooturl';\r\n";
		$pf				.= "\$pp_relrooturl =	'$pp_relrooturl';\r\n";
		$pf				.= "\$pp_language =	'$pp_lang';\r\n";
		$pf				.= "?>";

		$pf_filename	= $this->DeployPath .  'tiny_mce/plugins/tinybrowser/ppconfig.php';

		file_put_contents($pf_filename,$pf);

		$pages = PortalPageRecord::finder()->findAll("active=1");

		if($pages)
		{
			$extlink_js = "var tinyMCELinkList = new Array(";
			foreach($pages as $page)
			{
				if($page->path)
				{
					$name	= str_replace("." ,"/", $page->path) . ' / '.$page->displayname;
				}
				else
				{
					$name = $page->displayname;
				}
				$link	= $this->Service->constructUrl($page->fullpath);
				$extlink_js .= "[\"$name\",\"$link\"],";
			}

			$extlink_js = substr($extlink_js,0,-1);
			$extlink_js .= ");";

			$extlink_file	= str_replace('//','/',$this->DeployPath . '/myexternallist.js');
			file_put_contents($extlink_file,$extlink_js);
		}
		return $this->DeployURL;
	}
	

	protected function getEditorOptions()
	{
		$options	= parent::getEditorOptions();
		$assets		= $this->getApplication()->getAssetManager();
		$md5sum		= Prado::getPathOfNamespace('System.3rdParty.TinyMCE.tiny_mce','.md5');
		$fullpath	= realpath($md5sum);
		$dir		= sprintf('%x',crc32(dirname($fullpath) . Prado::getVersion()));
		$url		= $assets->getPublishedUrl($dir);

		$options['external_link_list_url']	= $this->getApplication()->getPortalBaseUrl()  . $assets->getBaseUrl() . '/' . $dir . '/myexternallist.js';
		$options['plugins'] = "style,layer,table,save,advhr,advimage,advlink,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template";
		$options['theme_advanced_buttons1'] = "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect";
        $options['theme_advanced_buttons2'] = "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor";
        $options['theme_advanced_buttons3'] = "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,media,advhr,|,ltr,rtl";
        $options['theme_advanced_buttons4'] = "cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,blockquote,insertfile,insertimage";
		$options['theme_advanced_resizing'] = true;
		$options['file_browser_callback'] = "tinyBrowser";
		$options['relative_urls']			= false;
		$options['remove_script_host']		= false;
		$app	= Prado::getApplication();
		$options['document_base_url']		= $app->Service->Request->BaseUrl;

		return $options;
	}

	public function getDeployPath()
	{
		return $this->_deploypath;
	}

	public function setDeployPath($p_path)
	{
		$retval = str_replace('\\','/',$p_path);
		if(substr($retval,-1) != '/')
		{
			$retval .= '/';
		}
		// Remove double //
		$retval = str_replace('//','/',$retval);
		$this->_deploypath = $retval;
	}

	public function getDeployURL()
	{
		return $this->_deployurl;
	}

	public function setDeployURL($p_url)
	{
		$this->_deployurl	= $p_url;
	}
}
?>